import { defineStore } from "pinia";
// import WebSocket from '@/utils/WebSocket';
import {useUserStore} from '@/stores/user'

export const useIMStore = defineStore("im", {
  // 开启数据缓存
  persist: {
    enabled: true,
  },
  state: () => {
    return {
      socket: <WebSocket>{},
      friends: [],
      user: useUserStore().userInfo
    };
  },
  getters: {
    
  },
  actions: {
    connect() {
      this.socket = new WebSocket(this.user.userId)
      this.socket.handleClose((event: any) => {
        console.log(event);
      })
    },
    send(to: number | string, message: string) {
      this.socket.send({
        type: 'message',
        from: this.user.userId,
        to: to,
        data: message
      })
    },
  },
});
